package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.airbnb.lottie.LottieAnimationView;
import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.StringUtil;
import com.lzy.okgo.OkGo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by TCL on 2019/6/6.
 * Describe:
 */
public class LottieActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.animation_view)
    LottieAnimationView animationView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_lottie;
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_lottie));
        toolbar.inflateMenu(R.menu.menu_lottie);
    }

    @Override
    protected void setListener() {
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.toolbar_menu) {
                    return true;
                } else {
                    String fileName;
                    switch (id) {
                        case R.id.toolbar2:
                            fileName = "bullseye.json";
                            break;
                        case R.id.toolbar3:
                            fileName = "walkthrough.json";
                            break;
                        case R.id.toolbar4:
                            fileName = "LottieLogo1.json";
                            break;
                        case R.id.toolbar5:

                            fileName = "LottieLogo2.json";
                            break;
                        case R.id.toolbar1:
                        default:
                            fileName = "AndroidWave.json";
                            break;
                    }
                    playLocalLottieAnimation(fileName);
                    return true;
                }
            }
        });

        //监听动画进度
        animationView.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                // 判断动画加载结束
                if (valueAnimator.getAnimatedFraction() == 1f) {
                    System.out.println("输出：动画播放结束");
                }
            }
        });
        //监听动画状态
        animationView.addAnimatorListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                System.out.println("输出动画开始");
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                System.out.println("输出动画结束");
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                System.out.println("输出动画取消");
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                System.out.println("输出动画重复");
            }
        });
    }

    private void playLocalLottieAnimation(String fileName) {
        if (StringUtil.checkIsNotNull(fileName)) {
            // 开启硬件加速
            animationView.useHardwareAcceleration(true);
            animationView.setAnimation(fileName);
            animationView.setRepeatCount(-1);
            animationView.playAnimation();
        } else {
            MyToast.toastError(LottieActivity.this, "动画文件出错", false);
        }
    }

    @OnClick({R.id.btn1, R.id.btn2})
    public void onViewClicked(View view) {
        String fileUrl;
        switch (view.getId()) {
            case R.id.btn1:
                default:
                fileUrl = "https://assets5.lottiefiles.com/packages/lf20_xjYU8S.json";
                break;
            case R.id.btn2:
                fileUrl = "https://assets2.lottiefiles.com/packages/lf20_tj5HET.json";
                break;
        }
        playNetLottieAnimation(fileUrl);
    }

    private void playNetLottieAnimation(String fileUrl) {
        if (StringUtil.checkIsNotNull(fileUrl)) {
            // 开启硬件加速
            animationView.useHardwareAcceleration(true);
            animationView.setAnimationFromUrl(fileUrl);
            animationView.setRepeatCount(-1);
            animationView.playAnimation();
        } else {
            MyToast.toastError(LottieActivity.this, "动画文件出错", false);
        }
    }

}
